package com.learnjava.learnjava1.controller;

import com.learnjava.learnjava1.dto.CompanyDTO;
import com.learnjava.learnjava1.service.CompanyService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;


@RestController
@AllArgsConstructor

@RequestMapping("/company")
public class CompanyController {

    private final CompanyService companyService;

    @Operation(summary = "Создание компании")
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public CompanyDTO create(@Valid @RequestBody CompanyDTO companyDto) {
        return companyService.create(companyDto);
    }

    @Operation(summary = "Получить компанию по id")
    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public CompanyDTO getCompanyById(@PathVariable Long id) {
        return companyService.getCompanyById(id);
    }

    @Operation(summary = "Получить список всех компаний")
    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public Page<CompanyDTO> readAll(@PageableDefault(value = 10, page = 0) Pageable pageable) {
        return companyService.readAll(pageable);
    }

    @Operation(summary = "Фильтрация")
    @GetMapping(path = "filter")
    @ResponseStatus(HttpStatus.OK)
    public Page<CompanyDTO> filterAll(@RequestParam(name = "title", required = false) String title,
                                      @RequestParam(name = "active", required = false) Boolean active,
                                      @RequestParam(name = "id", required = false) Long id,
                                      @PageableDefault(value = 10, page = 0) Pageable pageable) {
        return companyService.filterAll(title, active, id, pageable);
    }

    @Operation(summary = "Обновить компанию")
    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public CompanyDTO update(@RequestBody CompanyDTO companyDto, @PathVariable Long id) {
        return companyService.update(companyDto, id);
    }

    @Operation(summary = "Удалить компанию")
    @DeleteMapping("/{id}")
    public HttpStatus delete(@PathVariable Long id) {
        companyService.delete(id);
        return HttpStatus.OK;
    }

}
